"use strict";

import Vue from 'vue';
import axios from "axios";
// Full config:  https://github.com/axios/axios#request-config
axios.defaults.baseURL = process.env.NODE_ENV == 'development' ? 'http://wlgl.nanme.top/api/' : './api';
// axios.defaults.baseURL = 'http://px-tour.com/api/'

// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

Vue.prototype.$post = function (params, sure = false, fail = false, printTime = false) {

  let sendTime = 0;
  if (printTime) {
    sendTime = new Date;
    console.log('开始提交数据');
  }
  if (fail) {
    if (sure) return axios.post('q.php', params).then(res => {
      if (printTime) {
        console.log('响应耗时', (new Date - sendTime)/1000);
        if (res.data.logicTime) console.log(res.data.logicTime);
      }
      sure(res.data)
    }).catch(fail);
    return axios.post('q.php', params).catch(fail);
  } else {
    if (sure) {
      return axios.post('q.php', params).then(res => {
        if (printTime) {
          console.log('响应耗时', (new Date - sendTime)/1000);
          if (res.data.logicTime) console.log(res.data.logicTime);
        }
        sure(res.data)
      });
    } else {
      return axios.post('q.php', params);
    }
  }
}

let tmp = axios.defaults.baseURL;
Vue.prototype.$api =
  tmp.substr(-1) == "/" || tmp.substr(-1) == "\\"
    ? tmp.substr(0, tmp.length - 1)
    : tmp;

Vue.prototype.$isset = function (v) {
  return typeof v != 'undefined'
}
Object.defineProperty(Object.prototype, 'inType', {
  configurable: true,
  enumerable: false,
  writable: true,
  value: function (params) {
    if (Array.isArray(params)) {
      for (let i = 0; i < params.length; i++) {
        if (typeof this[params[i]] === 'undefined') return false;
      }
      return params.length > 0;
    } else if (typeof params === 'string') {
      return typeof this[params] != 'undefined';
    }
    return false;
  }
})
/* function (params) {
  if (Array.isArray(params)) {
    for (let i = 0; i < params.length; i++) {
      if (typeof this[params[i]] === 'undefined') return false;
    }
    return true;
  } else if (typeof params === 'string') {
    return typeof this[params] != 'undefined';
  }
  return false;
} */

function isObject(value) {
  const type = typeof value
  return value && ((type === 'object' && !(value instanceof Date)) || type === 'function')
}
Vue.prototype.$isObject = function (value) { return isObject(value) }
function clone(params) {
  if (isObject(params)) {
    let ret;
    if (Array.isArray(params)) {
      ret = params.map(a => {
        return clone(a);
      });
    } else {
      let keys = Object.keys(params);
      ret = {};
      keys.map(a => {
        ret[a] = clone(params[a]);
      });
    }
    return ret;
  }
  return params;
}
Vue.prototype.$clone = function (v) {
  return clone(v);
}
function del(v, keys = []) {
  if (typeof (keys) == 'string') keys = [keys];
  if (!keys.length) return {};
  let obj = JSON.parse(JSON.stringify(v));
  let ret = {};
  for (const key in obj) {
    if (function (k) {
      for (let i = 0; i < keys.length; i++) {
        if (k == keys[i]) {
          keys.splice(i, 1);
          return false;
        }
      }
      return true;
    }(key)) {
      ret[key] = obj[key];
    }
  }
  return ret;
}
Vue.prototype.$del = function (v, keys = ['changeTime', 'createTime', 'useTime']) {
  return del(v, keys)
};
function cp(obj, keys = false) {
  if (!obj) {
    if (typeof obj == 'undefined') return undefined;
    return obj;
  }
  if (!keys) return JSON.parse(JSON.stringify(obj));
  let ret = obj.map(a => {
    const ret = {};
    for (let i = 0; i < keys.length; i++) {
      ret[keys[i]] = a[keys[i]];
    }
    return ret;
  });
  return ret;
}

Vue.prototype.$cp = function (obj) {
  return clone(obj);
}
Vue.prototype.$GetLength = function (str) {
  ///<summary>获得字符串实际长度，中文2，英文1</summary>
  ///<param name="str">要获得长度的字符串</param>
  var realLength = 0, len = str.length, charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128)
      realLength += 1;
    else
      realLength += 2;
  }
  return realLength;
}
Array.prototype.id2item = function (params) {
  for (let index = 0; index < this.length; index++) {
    if (this[index].id == params) return this[index];
  }
}
function objCompare(v1, v2, sure = false) {
  const keys = Object.keys(v1);
  if (sure && keys.length != Object.keys(v2).length) return false;
  if (!keys.length) return !Object.keys(v2).length;
  let ret = true;
  for (let i = 0; i < keys.length; i++) {
    if (v1[keys[i]] != v2[keys[i]]) {
      if (typeof v1[keys[i]] == 'object' &&
        typeof v2[keys[i]] == 'object' &&
        objCompare(v1[keys[i]], v2[keys[i]])
      ) continue;
      ret = false;
      break;
    }
  }
  return ret;
}
Vue.prototype.$objCompare = function (v1, v2) {
  return objCompare(v1, v2);
}
function objCompare2(v1, v2) {
  const keys = Object.keys(v1);
  let ret = true;
  for (let i = 0; i < keys.length; i++) {
    if (v1[keys[i]] != v2[keys[i]]) {
      if (typeof v1[keys[i]] == 'object' &&
        typeof v2[keys[i]] == 'object' &&
        objCompare2(v1[keys[i]], v2[keys[i]])
      ) continue;

      ret = false;
      break;
    }
  }
  return ret;
}
Vue.prototype.$objCompare2 = function (v1, v2) {
  return objCompare2(v1, v2);
}



Array.prototype.v2item = function (params, key = 'id', retI = false) {
  for (let index = 0; index < this.length; index++) {
    if (this[index][key] == params) return retI ? index : this[index];
    if (!retI && this[index].children) {
      let tmp = this[index].children.v2item(params, key);
      if (tmp) return tmp;
    }
  }
  return retI ? -1 : false;
}
Array.prototype.getItem = function () {
  if (arguments.length < 1) return false;
  let o = typeof arguments[0];
  let retI = false;
  let sure = false;
  let v = {};
  if (o == 'number' || o == 'string') {
    o = false;
    if (typeof arguments[1] == 'string') {
      v[arguments[1]] = arguments[0];
    }
    if (typeof arguments[1] == 'undefined') {
      v.id = arguments[0];
    }
    if (arguments[2]) retI = true;
  }
  else {
    v = arguments[0];
    if (arguments[1]) retI = true;
    if (arguments[2]) sure = true;
  }
  for (let index = 0; index < this.length; index++) {
    if (objCompare(v, this[index], sure)) return retI ? index : this[index];
    if (!retI && this[index].children) {
      let tmp = this[index].children.getItem(v, retI);
      if (tmp) return tmp;
    }
  }
  return retI ? -1 : false;
}
function tree2arr(params, key = false) {
  let tmp = del(params, 'children');
  let ret = [key ? tmp[key] : tmp];
  if (Array.isArray(params.children)) {
    for (let i = 0; i < params.children.length; i++) {
      ret.push.apply(ret, tree2arr(params.children[i], key));
    }
  }
  return ret;
}
Vue.prototype.$tree2arr = function (params, key = false) {
  return tree2arr(params, key);
}
Array.prototype.getTreeID = function (id, key = 'pid', pushM = true) {
  let ret = [];
  for (let i = 0; i < this.length; i++) {
    if (this[i].id == id) {
      if (this[i][key]) {
        ret = this.getTreeID(this[i][key], key);
      }
      ret.push(id);
      break;
    }

  }
  return ret;
}
Array.prototype.add = function (array) {
  for (let i = 0; i < array.length; i++) {
    this.push(array[i]);
  }
}

Array.prototype.arr2tree = function (pid = 0, first = true) {
  // if (this.__ob__) console.time("a");
  // let arr = this.__ob__ ? JSON.parse(JSON.stringify(this)) : this;
  let arr = this.__ob__ ? clone(this) : this;
  // if (this.__ob__) console.timeEnd("a");
  let tree = [];
  let temp;
  let obj;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].pid == pid) {
      obj = arr[i];
      temp = arr.arr2tree(arr[i].id, false);
      if (temp.length > 0) {
        obj.children = temp;
      }
      tree.push(obj);
    }
  }

  if (first) {
    arr.map(a => {
      if (a.pid && !tree.getItem({ id: a.id })) tree.push(a);
      // console.log(tree.getItem({id:a.id}),a);
    });
  }
  return tree;
}
Array.prototype.arr2tree2 = function (v = 0, k = 'pid', fk = 'id', c = 'children') {
  let arr = JSON.parse(JSON.stringify(this));
  let tree = [];
  let temp;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i][k] == v) {
      let obj = arr[i];
      temp = arr.arr2tree2(arr[i][fk]);
      if (temp.length > 0) {
        obj[c] = temp;
      }
      tree.push(obj);
    }
  }
  return tree;
}

String.prototype.parse = function () {
  let ret = {};
  try {
    ret = JSON.parse(this);
  } catch (error) {
    ret = this;
  }
  return ret;
}
// Array.prototype.findItem = function (uid) {
//   let ret = [];
//   this.map(a => {
//     if (a.uid.indexOf(uid) >= 0) ret.push(a);
//   });
//   return ret;
// }

Vue.prototype.$userType = ['游客', '会员', '商家', '管理员'];
Vue.prototype.$gd = false;
var agd = {};
Vue.prototype.$setGD = function (params) {
  Vue.prototype.$gd = params;
  agd = Vue.prototype.$gd;
}
Vue.prototype.$formatGD = function (res) {
  // let t = {};
  // res.stock.map(a => {
  //   t = res.items.getItem({ id: a.item });
  //   a.name = t.name;
  //   a.uid = t.uid;
  //   a.model = t.model;
  //   t = res.project.getTreeID(a.project)[0];
  //   t = res.project.getItem(t);
  //   a.projectName = t.name;
  // });
  // res.project.map(a => {
  //   a.info = res.stock.filter(b => {
  //     if (b.project == a.id) return true;
  //   });
  // });
  Vue.prototype.$gd = res;
}
function objCMP(a, b) {
  if (typeof a == 'object' && typeof b == 'object') {
    let ka = Object.keys(a);
    let kb = Object.keys(b);
    if (ka.length == kb.length) {
      for (let i = 0; i < ka.length; i++) {
        if (a[ka[i]] != b[ka[i]]) {
          if (!(typeof a[ka[i]] == 'object' && typeof b[ka[i]] == 'object' && objCMP(a[ka[i]], a[ka[i]]))) return false;
        }
      }
      return true;
    }
  }
  return false;
}
Vue.prototype.$objCMP = function (a, b) {
  return objCMP(a, b);
}

// Vue.use(Plugin)

// export default Plugin;
